package shapewar.media;

import java.io.IOException;
import java.util.HashMap;

import shapewar.activities.R;
import android.content.Context;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.SoundPool;

public class Media {
	private static MediaPlayer mPlayer;
	private static SoundPool soundPool;
	private static HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
	public static void init(Context context){
		soundPool = new SoundPool(3,AudioManager.STREAM_MUSIC,5);
		map.put(1, soundPool.load(context, R.raw.shot,1));
		map.put(2, soundPool.load(context, R.raw.flash,1));
		map.put(3, soundPool.load(context, R.raw.boom,1));
		
	}
	public static void play(Context context){
		try {
			mPlayer = new MediaPlayer();
			AssetManager am = context.getAssets();
        	try {
				mPlayer.setDataSource(am.openFd("bgm.mp3").getFileDescriptor());
				mPlayer.setLooping(true);
				mPlayer.setOnPreparedListener(new OnPreparedListener() {
					@Override
					public void onPrepared(MediaPlayer mPlayer) {
						mPlayer.start();
					}
				});
				mPlayer.prepareAsync();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}
	public static void release(){
		mPlayer.release();
	}
	
	public static void fire(float dis,float max_dis){
		float vm = max_dis / dis;
		if(vm>1)vm = 1;
		soundPool.play(map.get(1), vm*0.3f, vm*0.3f, 1, 0, 1);
	}
	
	public static void flash(float dis,float max_dis) {
		float vm = max_dis / dis;
		if(vm>1)vm = 1;
		soundPool.play(map.get(2), vm*0.3f, vm*0.3f, 1, 0, 1);
	}
	
	public static void boom(float dis,float max_dis){
		float vm = max_dis / dis;
		if(vm>1)vm = 1;
		soundPool.play(map.get(3), vm*0.3f, vm*0.3f, 1, 0, 1);
	}
}
